import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../layout/Home.vue'
import local from '../utils/locl.js'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  // 根目录设置默认地址
  {
    path: '/',
    redirect: '/home'
  },
  // 首页
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/backstage',
    children: [
      {
        // 后台首页
        path: '/backstage',
        component: () =>
          import(
            /* webpackChunkName: "backstage" */ '../views/backstage/index.vue'
          )
      }
    ]
  },
  // 登录页
  {
    path: '/login',
    component: () =>
      import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  // 404
  {
    path: '*',
    redirect: '/404'
  },
  {
    path: '/404',
    component: () =>
      import(/* webpackChunkName: "login" */ '../views/error/index.vue')
  }
]

// component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')

const routerPath = {
  path: '/home',
  name: 'Home',
  component: Home,
  redirect: '/backstage',
  children: [
    {
      // 后台首页
      path: '/backstage',
      meta: { name: '首页' },
      isVisible: true,
      component: () =>
        import(
          /* webpackChunkName: "backstage" */ '../views/backstage/index.vue'
        )
    },
    {
      // 订单管理
      path: '/order',
      meta: { name: '订单管理', role: ['super', 'normal'] },
      component: () =>
        import(/* webpackChunkName: "login" */ '../views/order/index.vue'),
      redirect: '/order/list',
      isVisible: true,
      children: [
        {
          path: '/order/list',
          meta: { name: '订单首页' },
          isVisible: true,
          component: () =>
            import(/* webpackChunkName: "login" */ '../views/order/list.vue')
        },
        {
          path: '/order/bianji',
          meta: { name: '编辑订单', role: ['super'] },
          component: () =>
            import(/* webpackChunkName: "login" */ '../views/order/bianji.vue')
        }
      ]
    },
    {
      // 商品管理
      path: '/commodity',
      meta: { name: '商品管理' },
      component: () =>
        import(/* webpackChunkName: "login" */ '../views/commodity/index.vue'),
      redirect: '/commodity/list',
      isVisible: true,
      children: [
        {
          path: '/commodity/list',
          meta: { name: '商品列表' },
          isVisible: true,
          component: () =>
            import(
              /* webpackChunkName: "login" */ '../views/commodity/list.vue'
            )
        },
        {
          path: '/commodity/add',
          meta: { name: '商品添加' },
          isVisible: true,
          component: () =>
            import(/* webpackChunkName: "login" */ '../views/commodity/add.vue')
        },
        {
          path: '/commodity/fen',
          meta: { name: '商品分类' },
          isVisible: true,
          component: () =>
            import(/* webpackChunkName: "login" */ '../views/commodity/fen.vue')
        }
      ]
    },
    {
      // 店铺管理
      path: '/store',
      meta: { name: '店铺管理', role: ['super'] },
      component: () =>
        import(/* webpackChunkName: "login" */ '../views/store/index.vue')
    },
    {
      // 账号管理
      path: '/number',
      meta: { name: '账号管理', role: ['super', 'normal'] },
      isVisible: true,
      component: () =>
        import(/* webpackChunkName: "login" */ '../views/number/index.vue'),
      redirect: '/number/list',
      isVisible: true,
      children: [
        {
          path: '/number/list',
          meta: { name: '账号列表', role: ['super'] },
          isVisible: true,
          component: () =>
            import(/* webpackChunkName: "login" */ '../views/number/list.vue')
        },
        {
          path: '/number/add',
          meta: { name: '添加账号', role: ['super'] },
          component: () =>
            import(/* webpackChunkName: "login" */ '../views/number/add.vue')
        },
        {
          path: '/number/modify',
          meta: { name: '修改密码' },
          isVisible: true,
          component: () =>
            import(/* webpackChunkName: "login" */ '../views/number/modify.vue')
        },
        {
          path: '/number/person',
          meta: { name: '个人中心' },
          component: () =>
            import(/* webpackChunkName: "login" */ '../views/number/person.vue')
        }
      ]
    },
    {
      // 销售统计
      path: '/Statistics',
      meta: { name: '销售统计', role: ['super'] },
      component: () =>
        import(/* webpackChunkName: "login" */ '../views/Statistics/index.vue'),
      redirect: '/Statistics/goods',
      children: [
        {
          path: '/Statistics/goods',
          meta: { name: '商品统计' },
          component: () =>
            import(
              /* webpackChunkName: "login" */ '../views/Statistics/goods.vue'
            )
        },
        {
          path: '/Statistics/order_form',
          meta: { name: '订单统计' },
          component: () =>
            import(
              /* webpackChunkName: "login" */ '../views/Statistics/order_form.vue'
            )
        }
      ]
    }
  ]
}

const router = new VueRouter({
  routes
})

cRouter()
export function cRouter() {
  const role = local.get('role')
  if (!role) return
  // 获取过滤出来的路由
  const filterR = filterFn(routerPath.children, role)
  console.log(filterR, 123)
  // 将新的给旧的
  // routerPath.children = filterR
  // 合并
  router.addRoutes([routerPath])

  const navMenu = cMenu(filterR)
  local.set('showmenu', JSON.stringify(navMenu))
}

function cMenu(menu) {
  return menu.filter(item => {
    if (item.isVisible) {
      if (item.children && item.children.length > 0) {
        item.children = cMenu(item.children)
      }
      return true
    } else {
      return false
    }
  })
}

// 主要做功能的函数
function filterFn(router, role) {
  // 过滤。 将满足的条件都放到一个新的数组中
  return router.filter(item => {
    if (hasPromiss(item, role)) {
      if (item.children && item.children.length > 0) {
        item.children = filterFn(item.children, role)
      }
      return true
    } else {
      return false
    }
  })
}
// 鉴定函数，判断当前路由是否拥有权限
function hasPromiss(item, r) {
  // 开始真正的对比代码
  if (item.meta && item.meta.role) {
    return item.meta.role.includes(r)
  } else {
    return true
  }
}

// 导航守卫，目的是不允许未登录的用户进入路由页面
router.beforeEach((to, from, next) => {
  const token = local.get('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
